//Erstellt von Moritz Liepe
package main.editor;

import main.IOControl;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;


public class SetSourceHandler extends AbstractHandler
{

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException 
	{
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) 
		{
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			if(! strucSelection.isEmpty() && strucSelection.getFirstElement() instanceof IOControl)
			{
				IOControl selectionObject = (IOControl)strucSelection.getFirstElement();
				String message = "";
				
				VOSEditor vosEditor;
				IEditorPart page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActiveEditor();
				if((page != null) && (page instanceof VOSEditor))
				{
					vosEditor = (VOSEditor)page;
					message = vosEditor.getModuleEditor().setSource(selectionObject);
				}
				Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();  
				String dialogBoxTitle = "Result";  
				MessageDialog.openInformation(shell, dialogBoxTitle, message); 
			}
		}
		 
		
		return null;
	}

}
